"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = require("lodash");
const enum_1 = require("../typing/enum");
const helper_1 = require("../common/helper");
const utils_1 = require("../common/utils");
// 查找出现牌最多的牌
function findMostFrequent(arr) {
    const count = {};
    let maxNum = 1;
    let maxCount = 0;
    for (let i = 0; i < arr.length; i++) {
        const num = arr[i];
        if ((0, helper_1.isJoker)(num))
            continue;
        const val = (0, helper_1.getCardValue)(num);
        count[val] = [...(count[val] || []), num];
        if (count[val].length > maxCount) {
            maxNum = val;
            maxCount = count[val].length;
        }
    }
    return {
        value: maxNum,
        origins: count[maxNum]
    };
}
// 查找出现牌最少的牌
function findLeast(arr) {
    const count = {};
    let maxKey = '';
    let maxCount = 4;
    for (let i = 0; i < arr.length; i++) {
        const num = arr[i];
        if ((0, helper_1.isJoker)(num))
            continue;
        const val = (0, helper_1.getCardValue)(num);
        count[val] = [...(count[val] || []), num];
    }
    for (let key in count) {
        if (count[key].length <= maxCount) {
            maxKey = key;
            if (count[key].length === 1) {
                break;
            }
        }
    }
    return {
        value: maxKey,
        origins: count[maxKey]
    };
}
const Moves = [{
        value: enum_1.OP_STATUS.PLAY,
        weight: 50
    }, {
        value: enum_1.OP_STATUS.LIFT,
        weight: 30
    }, {
        value: enum_1.OP_STATUS.PASS,
        weight: 20
    }];
// TODO 考虑之前出的
class Robot {
    constructor(sessionId, room) {
        this.sessionId = sessionId;
        this.room = room;
    }
    // 出牌
    play(delayTime) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            // 机器人选牌和出牌
            let leftCards = ((_a = this.room.state.players.get(this.sessionId)) === null || _a === void 0 ? void 0 : _a.leftCards) || [];
            const { currentGuessCard } = this.room.state;
            yield this.room.delay((0, lodash_1.isNumber)(delayTime) ? (0, lodash_1.random)(2, 5) * 1000 : delayTime);
            // 并不是判断这个就行
            if (!currentGuessCard) {
                const { value, origins } = findMostFrequent(leftCards);
                const count = (0, lodash_1.random)(1, origins.length);
                this.room.onPlay({ sessionId: this.sessionId, playCards: (0, lodash_1.sampleSize)(origins, count), guessCardId: String(value) });
                return;
            }
            // 相同的牌
            const sameValueCard = leftCards.filter(item => (0, helper_1.isSameCardValue)(item, currentGuessCard));
            // 万能牌
            const purposeCard = leftCards.filter(item => (0, helper_1.isJoker)(item));
            const sameValueCardCount = sameValueCard.length;
            const purposeCardCount = purposeCard.length;
            const totalCount = sameValueCardCount + purposeCardCount;
            const hasSameCard = totalCount > 0;
            const lastCards = this.room.lastPlayedCards;
            // 有牌
            if (hasSameCard) {
                // 包括两张万能牌,超出牌数
                if (totalCount + lastCards.length > 6) {
                    // 掀牌
                    this.room.onLift({ sessionId: this.sessionId });
                }
                else {
                    // 有牌
                    // 随机出几张
                    const playCount = (0, lodash_1.random)(1, Math.min(4, totalCount));
                    const cardsPool = playCount <= sameValueCardCount ? sameValueCard : [...sameValueCard, ...purposeCard];
                    this.room.onPlay({ sessionId: this.sessionId, playCards: (0, lodash_1.sampleSize)(cardsPool, playCount) });
                }
            }
            else {
                const action = (0, utils_1.getValueByWeight)(Moves);
                switch (action) {
                    case enum_1.OP_STATUS.PLAY: {
                        const { origins } = findLeast(leftCards);
                        this.room.onPlay({ sessionId: this.sessionId, playCards: origins });
                        break;
                    }
                    case enum_1.OP_STATUS.LIFT: {
                        // 掀牌
                        this.room.onLift({ sessionId: this.sessionId });
                        break;
                    }
                    case enum_1.OP_STATUS.PASS: {
                        // 掀牌
                        this.room.onPass({ sessionId: this.sessionId });
                        break;
                    }
                    default: {
                        // 掀牌
                        this.room.onPass({ sessionId: this.sessionId });
                        break;
                    }
                }
            }
        });
    }
}
exports.default = Robot;
